package com.apes.fn.scm.crossCompany.transfer.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.account.tax.service.TaxService;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 *
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BatchDumpItem.CONST_TABLE_NAME)
public class BatchDumpItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "stock_batch_dump_item";
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "批量转储明细")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "batch_dump_id", referencedColumnName = "id")
    private BatchDump batchDump;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "receipt_location_id", referencedColumnName = "id")
    @Comment("收货仓库")
    private Location receiptLocation;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;

/*    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;*/

    @Column(name = "product_sum", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("数量")
    private double productSum = 0;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "downstream_document")
    @Comment("下游单号")
    private String downstreamDocument;

    @Column(name = "downstream_document_type")
    @Comment("下游单号")
    private String downstreamDocumentType;


    @Transient
    private double factor;

    public double getFactor() {
        if (uom != null) return 1/uom.getFactor();
        return 0;
    }

}
